package gwtgaetools.client.ui;

import gwtgaetools.client.ItemManager;
import gwtgaetools.client.listeners.Listener;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.text.shared.Renderer;
import com.google.gwt.user.client.ui.ValueListBox;
import com.google.gwt.view.client.ProvidesKey;

public class ClickValueListBox<T> extends ValueListBox<T> implements HasClickHandlers {

	public ClickValueListBox(Renderer<T> renderer, ProvidesKey<T> keyProvider) {
		super(renderer, keyProvider);
	}

	public ClickValueListBox(Renderer<T> renderer, ItemManager<T> type, List<T> list) {
		super(renderer);
		addClickHandler(new ClickHandler() {
			@Override public void onClick(ClickEvent event) {
				event.stopPropagation();
			}
		});
		type.registerListener(new Listener<ItemManager<T>>() {
			@Override public void observe(ItemManager<T> t) {
				setValue(t.getItem());
			}
		});
		setAcceptableValues(list);
	}

	@Override
	public HandlerRegistration addClickHandler(ClickHandler handler) {
		return addDomHandler(handler, ClickEvent.getType());
	}

}
